﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using CE.iPhone.PList;
using System.Resources;
namespace cczEdit
{


   
    public partial class UCItemData : UserControl
    {
        List<Sundry> sundries;
        List<Equipment> weapons;
        List<Equipment> armors;
        List<Equipment> assists;
        PListRoot root;
        String fileName;
        String resourcesDirect;
        public UCItemData()
        {
            InitializeComponent();
        }
        public UCItemData(String cczDirector_)
        {

            InitializeComponent();
            listView1.Columns.Add("ID", 40, HorizontalAlignment.Left);
            listView1.Columns.Add("名称", 120, HorizontalAlignment.Left);
            fileName = cczDirector_+"\\ItemData.plist";
            resourcesDirect = cczDirector_+"\\Item\\";
            sundries = new List<Sundry>();
            weapons = new List<Equipment>();
            armors = new List<Equipment>();
            assists = new List<Equipment>();
            LoadData(fileName);
            sundries.Sort(Sundry.compare);
            weapons.Sort(Sundry.compare);
            armors.Sort(Sundry.compare);
            assists.Sort(Sundry.compare);
            
            ShowWeapons();
            
        }
        private void LoadData(String fileName_)
        {

            root = PListRoot.Load(fileName_);
            PListDict rootDic = (PListDict)root.Root;
            PListDict weaponsDic = (PListDict)rootDic["Weapon"];
            PListDict armorsDic = (PListDict)rootDic["Armor"];
            PListDict assistsDic = (PListDict)rootDic["Assist"];
            PListDict SundrisDic = (PListDict)rootDic["Sundry"];
            foreach (KeyValuePair<String, IPListElement> a in weaponsDic)
            {
                PListDict weaponDic = (PListDict)a.Value;
                PListString name = (PListString)weaponDic["Name"];

                PListInteger num = (PListInteger)weaponDic["Num"];
                PListInteger numIcon = (PListInteger)weaponDic["NumIcon"];

                PListString dspt = (PListString)weaponDic["Dspt"];
                PListInteger grow = (PListInteger)weaponDic["Grow"];
                PListInteger numEffect = (PListInteger)weaponDic["NumEffect"];
                PListInteger valueEffect = (PListInteger)weaponDic["ValueEffect"];
                PListInteger price = (PListInteger)weaponDic["Price"];
                PListInteger type = (PListInteger)weaponDic["Type"];
                PListInteger valueAbility = (PListInteger)weaponDic["ValueAbility"];

                Equipment weapon = new Equipment();
                weapon.name = name.Value;
                weapon.num = num.Value;
                weapon.dspt = dspt.Value;
                weapon.numEffect = numEffect.Value;
                weapon.valueEffect = valueEffect.Value;
                weapon.numIcon = Convert.ToInt64(numIcon.Value);
                weapon.price = price.Value;

                weapon.grow = grow.Value;
                weapon.teamEnable = 0;
                weapon.type = type.Value;
                weapon.valueAbility = valueAbility.Value;
                weapons.Add(weapon);

            }
            foreach (KeyValuePair<String, IPListElement> a in armorsDic)
            {
                PListDict armorDic = (PListDict)a.Value;
                PListString name = (PListString)armorDic["Name"];

                PListInteger num = (PListInteger)armorDic["Num"];
                PListInteger numIcon = (PListInteger)armorDic["NumIcon"];

                PListString dspt = (PListString)armorDic["Dspt"];
                PListInteger grow = (PListInteger)armorDic["Grow"];
                PListInteger numEffect = (PListInteger)armorDic["NumEffect"];
                PListInteger valueEffect = (PListInteger)armorDic["ValueEffect"];
                PListInteger price = (PListInteger)armorDic["Price"];
                PListInteger type = (PListInteger)armorDic["Type"];
                PListInteger valueAbility = (PListInteger)armorDic["ValueAbility"];

                Equipment armor = new Equipment();
                armor.name = name.Value;
                armor.num = num.Value;
                armor.dspt = dspt.Value;
                armor.numEffect = numEffect.Value;
                armor.valueEffect = valueEffect.Value;
                armor.numIcon = Convert.ToInt64(numIcon.Value);
                armor.price = price.Value;

                armor.grow = grow.Value;
                armor.teamEnable = 0;
                armor.type = type.Value;
                armor.valueAbility = valueAbility.Value;
                armors.Add(armor);
            }
            foreach (KeyValuePair<String, IPListElement> a in assistsDic)
            {
                PListDict assistDic = (PListDict)a.Value;
                PListString name = (PListString)assistDic["Name"];

                PListInteger num = (PListInteger)assistDic["Num"];
                PListInteger numIcon = (PListInteger)assistDic["NumIcon"];
                PListInteger teamEnable = (PListInteger)assistDic["TeamEnable"];
                PListString dspt = (PListString)assistDic["Dspt"];
                PListInteger numEffect = (PListInteger)assistDic["NumEffect"];
                PListInteger valueEffect = (PListInteger)assistDic["ValueEffect"];
                PListInteger price = (PListInteger)assistDic["Price"];

                PListInteger valueAbility = (PListInteger)assistDic["ValueAbility"];

                Equipment assist = new Equipment();
                assist.name = name.Value;
                assist.num = num.Value;
                assist.dspt = dspt.Value;
                assist.numEffect = numEffect.Value;
                assist.valueEffect = valueEffect.Value;
                assist.numIcon = Convert.ToInt64(numIcon.Value);
                assist.price = price.Value;
                assist.teamEnable = teamEnable.Value;
                assist.grow = 0;


                assist.valueAbility = valueAbility.Value;
                assists.Add(assist);
            }
            foreach (KeyValuePair<String, IPListElement> a in SundrisDic)
            {
                PListDict sundryDic = (PListDict)a.Value;
                PListString name = (PListString)sundryDic["Name"];

                PListInteger num = (PListInteger)sundryDic["Num"];
                PListInteger numIcon = (PListInteger)sundryDic["NumIcon"];
                PListString dspt = (PListString)sundryDic["Dspt"];
                PListInteger numEffect = (PListInteger)sundryDic["NumEffect"];
                PListInteger valueEffect = (PListInteger)sundryDic["ValueEffect"];
                PListInteger price = (PListInteger)sundryDic["Price"];



                Equipment sundry = new Equipment();
                sundry.name = name.Value;
                sundry.num = num.Value;
                sundry.dspt = dspt.Value;
                sundry.numEffect = numEffect.Value;
                sundry.valueEffect = valueEffect.Value;
                sundry.numIcon = numIcon.Value;
                sundry.price = price.Value;

                sundry.grow = 0;
                sundry.teamEnable = 0;
                sundry.type = 0;
                sundry.valueAbility = 0;
                sundries.Add(sundry);

            }
        }
        private void ShowWeapons ()
        {
            listView1.Clear();
            listView1.Columns.Add("ID", 40, HorizontalAlignment.Left);
            listView1.Columns.Add("名称", 120, HorizontalAlignment.Left);
            foreach(Equipment equipment in weapons)
            {
                ListViewItem item = new ListViewItem(new String[] { equipment.num + "", equipment.name });
                listView1.Items.Add(item);
            }
            groupBox1.Visible = true;
            listView1.Focus();
            listView1.Items[0].Selected = true;
        }
        private void ShowArmors()
        {
            listView1.Clear();
            listView1.Columns.Add("ID", 40, HorizontalAlignment.Left);
            listView1.Columns.Add("名称", 120, HorizontalAlignment.Left);

            foreach (Equipment equipment in armors)
            {
                ListViewItem item = new ListViewItem(new String[] { equipment.num + "", equipment.name });
                listView1.Items.Add(item);
            }
            groupBox1.Visible = true;
            listView1.Focus();
            listView1.Items[0].Selected = true;
        }
        private void ShowAssists()
        {
            listView1.Clear();
            listView1.Columns.Add("ID", 40, HorizontalAlignment.Left);
            listView1.Columns.Add("名称", 120, HorizontalAlignment.Left);
            foreach (Equipment equipment in assists)
            {
                ListViewItem item = new ListViewItem(new String[] { equipment.num + "", equipment.name });
                listView1.Items.Add(item);
            }
            groupBox1.Visible = true;
            listView1.Focus();
            listView1.Items[0].Selected = true;
        }
        private void ShowSundris()
        {
            listView1.Clear();
            listView1.Columns.Add("ID", 40, HorizontalAlignment.Left);
            listView1.Columns.Add("名称", 120, HorizontalAlignment.Left);
            foreach (Sundry sundry in sundries)
            {
                ListViewItem item = new ListViewItem(new String[] { sundry.num + "", sundry.name });
                listView1.Items.Add(item);
            }
            groupBox1.Visible = false;
            listView1.Focus();
            listView1.Items[0].Selected = true;
        }

        void ShowEquipmentDetail(int ID)
        {
            Equipment equipment;
            if(radioButtonWeapon.Checked)
                equipment = weapons.Find((h) => { return h.num == ID; });
            else if(radioButtonArmor.Checked)
                equipment =armors.Find((h) => { return h.num == ID; });
            else
                equipment = assists.Find((h) => { return h.num == ID; });
            
            textBoxName.Text = equipment.name;
            textBoxPrice.Text = equipment.price.ToString();
            comboBoxEffect.Items.Clear();
            comboBoxEffect.Text =Constant.equipmentEffectDic[(int)equipment.numEffect];
            foreach (KeyValuePair<int, String> kv in Constant.equipmentEffectDic)
            {
                comboBoxEffect.Items.Add(kv.Value);
            }
            textBoxEffectValue.Text = equipment.valueEffect.ToString();

            numericUpDownIcon.Value = equipment.numIcon;
            pictureBoxIcon.Image = Image.FromFile(resourcesDirect+"Item" + equipment.numIcon + ".png");
            textBoxDspt.Text = equipment.dspt;
            textBoxAbilityValue.Text = equipment.valueAbility.ToString();
            textBoxGrow.Text = equipment.grow.ToString();
            if (radioButtonAssist.Checked)
            {
                comboBoxTeam.Enabled = true;
                comboBoxTeam.Items.Clear();
                comboBoxTeam.Text = Constant.teamTypeDic[(int)equipment.teamEnable];
                foreach (KeyValuePair<int, String> kv in Constant.teamTypeDic)
                {
                    comboBoxTeam.Items.Add(kv.Value);
                }
                
            }
            else
            {
                comboBoxTeam.Enabled = false;
                comboBoxType.Enabled = true;
                comboBoxType.Items.Clear();

                if (radioButtonArmor.Checked)
                {
                    comboBoxType.Text = Constant.armorTypeDic[(int)equipment.type];
                    foreach (KeyValuePair<int, String> kv in Constant.armorTypeDic)
                    {
                        comboBoxType.Items.Add(kv.Value);
                    }
                }
                if (radioButtonWeapon.Checked)
                {
                    comboBoxType.Text = Constant.weaponTypeDic[(int)equipment.type];
                    foreach (KeyValuePair<int, String> kv in Constant.weaponTypeDic)
                    {
                        comboBoxType.Items.Add(kv.Value);
                    }
                }
            }
             
            
        }
        void ShowSundryDetail(int ID)
        {
            Sundry sundry = sundries.Find((h) => { return h.num == ID; });;

            textBoxName.Text = sundry.name;
            textBoxPrice.Text = sundry.price.ToString();
            comboBoxEffect.Items.Clear();
            comboBoxEffect.Text = Constant.sundryEffectDic[(int)sundry.numEffect];
            foreach (KeyValuePair<int, String> kv in Constant.sundryEffectDic)
            {
                comboBoxEffect.Items.Add(kv.Value);
            }
            textBoxEffectValue.Text = sundry.valueEffect.ToString();
            numericUpDownIcon.Value = sundry.numIcon;
            pictureBoxIcon.Image = Image.FromFile(resourcesDirect + "Item" + sundry.numIcon + ".png");

            textBoxDspt.Text = sundry.dspt;
        }
        public void SaveAllData()
        {
            if (listView1.SelectedItems.Count == 0)
            {
                return;
            }
            ListViewItem item = listView1.SelectedItems[0];
            if (radioButtonWeapon.Checked)
                SaveWeaponDetail(Convert.ToInt32(item.SubItems[0].Text));
            else if (radioButtonArmor.Checked)
                SaveArmorDetail(Convert.ToInt32(item.SubItems[0].Text));
            else if (radioButtonAssist.Checked)
                SaveAssistDetail(Convert.ToInt32(item.SubItems[0].Text));
            else if (radioButtonConsume.Checked)
                SaveSundryDetail(Convert.ToInt32(item.SubItems[0].Text));
            root.Save(fileName);

        }
        

        void SaveWeaponDetail(int index)
        {
            Equipment equipment = weapons.Find((h) => { return h.num == index; });
            equipment.name = textBoxName.Text;
            equipment.price =Convert.ToInt64( textBoxPrice.Text);
            foreach (KeyValuePair<int, String> kv in Constant.equipmentEffectDic)
            {
                if(kv.Value== comboBoxEffect.Text){
                    equipment.numEffect = kv.Key;
                }
            }
            
            equipment.valueEffect=Convert.ToInt64(textBoxEffectValue.Text) ;
            equipment.numIcon = Convert.ToInt64(numericUpDownIcon.Value);
            equipment.valueAbility = Convert.ToInt64(textBoxAbilityValue.Text);
            equipment.dspt =textBoxDspt.Text;
            equipment.grow = Convert.ToInt64(textBoxGrow.Text);
            foreach (KeyValuePair<int, String> kv in Constant.weaponTypeDic)
            {
                if (kv.Value == comboBoxType.Text)
                {
                    equipment.type = kv.Key;
                }
            }
            PListDict rootDic = (PListDict)root.Root;
            PListDict weaponsDic = (PListDict)rootDic["Weapon"];
            foreach (KeyValuePair<String, IPListElement> a in weaponsDic)
            {
                PListDict weaponDic = (PListDict)a.Value;
                PListInteger num = (PListInteger)weaponDic["Num"];
                if (num.Value == equipment.num)
                {
                    weaponDic["Name"] = new PListString(equipment.name);
                    weaponDic["Dspt"] = new PListString(equipment.dspt);
                    weaponDic["NumEffect"] = new PListInteger(equipment.numEffect);
                    weaponDic["ValueEffect"] = new PListInteger(equipment.valueEffect);
                    weaponDic["ValueAbility"] = new PListInteger(equipment.valueAbility);
                    weaponDic["NumIcon"] = new PListInteger(equipment.numIcon);
                    weaponDic["Grow"] = new PListInteger(equipment.grow);
                    weaponDic["Type"] = new PListInteger(equipment.type);
                    weaponDic["Price"] = new PListInteger(equipment.price);
                }
            }
        }
        void SaveArmorDetail(int index)
        {
            Equipment equipment = armors.Find((h) => { return h.num == index; });
            equipment.name = textBoxName.Text;
            equipment.price = Convert.ToInt64(textBoxPrice.Text);
            foreach (KeyValuePair<int, String> kv in Constant.equipmentEffectDic)
            {
                if (kv.Value == comboBoxEffect.Text)
                {
                    equipment.numEffect = kv.Key;
                }
            }

            equipment.valueEffect = Convert.ToInt64(textBoxEffectValue.Text);
            equipment.numIcon = Convert.ToInt64(numericUpDownIcon.Value);
            equipment.valueAbility = Convert.ToInt64(textBoxAbilityValue.Text);
            equipment.dspt = textBoxDspt.Text;
            equipment.grow = Convert.ToInt64(textBoxGrow.Text);
            foreach (KeyValuePair<int, String> kv in Constant.armorTypeDic)
            {
                if (kv.Value == comboBoxType.Text)
                {
                    equipment.type = kv.Key;
                }
            }
            PListDict rootDic = (PListDict)root.Root;
            PListDict armorsDic = (PListDict)rootDic["Armor"];
            foreach (KeyValuePair<String, IPListElement> a in armorsDic)
            {
                PListDict armorDic = (PListDict)a.Value;
                PListInteger num = (PListInteger)armorDic["Num"];
                if (num.Value == equipment.num)
                {
                    armorDic["Name"] = new PListString(equipment.name);
                    armorDic["Dspt"] = new PListString(equipment.dspt);
                    armorDic["NumEffect"] = new PListInteger(equipment.numEffect);
                    armorDic["ValueEffect"] = new PListInteger(equipment.valueEffect);
                    armorDic["ValueAbility"] = new PListInteger(equipment.valueAbility);
                    armorDic["NumIcon"] = new PListInteger(equipment.numIcon);
                    armorDic["Grow"] = new PListInteger(equipment.grow);
                    armorDic["Type"] = new PListInteger(equipment.type);
                    armorDic["Price"] = new PListInteger(equipment.price);

                }
            }
        }
        void SaveAssistDetail(int index)
        {
            Equipment equipment = assists.Find((h) => { return h.num == index; });
            equipment.name = textBoxName.Text;
            equipment.price = Convert.ToInt64(textBoxPrice.Text);
            foreach (KeyValuePair<int, String> kv in Constant.equipmentEffectDic)
            {
                if (kv.Value == comboBoxEffect.Text)
                {
                    equipment.numEffect = kv.Key;
                }
            }
            foreach (KeyValuePair<int, String> kv in Constant.teamTypeDic)
            {
                if (kv.Value == comboBoxTeam.Text)
                {
                    equipment.teamEnable = kv.Key;
                }
            }
            equipment.valueEffect = Convert.ToInt64(textBoxEffectValue.Text);
            equipment.numIcon = Convert.ToInt64(numericUpDownIcon.Value);
            equipment.valueAbility = Convert.ToInt64(textBoxAbilityValue.Text);
            equipment.dspt = textBoxDspt.Text;
            equipment.grow = Convert.ToInt64(textBoxGrow.Text);
            PListDict rootDic = (PListDict)root.Root;
            PListDict assistsDic = (PListDict)rootDic["Assist"];
            foreach (KeyValuePair<String, IPListElement> a in assistsDic)
            {
                PListDict assistDic = (PListDict)a.Value;
                PListInteger num = (PListInteger)assistDic["Num"];
                if (num.Value == equipment.num)
                {
                    assistDic["Name"] = new PListString(equipment.name);
                    assistDic["Dspt"] = new PListString(equipment.dspt);
                    assistDic["NumEffect"] = new PListInteger(equipment.numEffect);
                    assistDic["ValueEffect"] = new PListInteger(equipment.valueEffect);
                    assistDic["ValueAbility"] = new PListInteger(equipment.valueAbility);
                    assistDic["NumIcon"] = new PListInteger(equipment.numIcon);
                    assistDic["Grow"] = new PListInteger(equipment.grow);
                    assistDic["Price"] = new PListInteger(equipment.price);
                    assistDic["TeamEnable"] = new PListInteger(equipment.teamEnable);
                }
            }
        }
        void SaveSundryDetail(int index)
        {
            Sundry sundry = sundries.Find((h) => { return h.num == index; });
            sundry.name = textBoxName.Text;
            sundry.price = Convert.ToInt64(textBoxPrice.Text);
            foreach (KeyValuePair<int, String> kv in Constant.sundryEffectDic)
            {
                if (kv.Value == comboBoxEffect.Text)
                {
                    sundry.numEffect = kv.Key;
                }
            }
            sundry.valueEffect = Convert.ToInt64(textBoxEffectValue.Text);
            sundry.numIcon = Convert.ToInt64(numericUpDownIcon.Value);
            sundry.dspt = textBoxDspt.Text;
            PListDict rootDic = (PListDict)root.Root;
            PListDict sundrisDic = (PListDict)rootDic["Sundry"];
            foreach (KeyValuePair<String, IPListElement> a in sundrisDic)
            {
                PListDict sundryDic = (PListDict)a.Value;
                PListInteger num = (PListInteger)sundryDic["Num"];
                if (num.Value == sundry.num)
                {
                    sundryDic["Name"] = new PListString(sundry.name);
                    sundryDic["Dspt"] = new PListString(sundry.dspt);
                    sundryDic["NumEffect"] = new PListInteger(sundry.numEffect);
                    sundryDic["ValueEffect"] = new PListInteger(sundry.valueEffect);
                    sundryDic["NumIcon"] = new PListInteger(sundry.numIcon);
                    sundryDic["Price"] = new PListInteger(sundry.price);

                }
            }
        }

        private void buttonSave_Click(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count == 0)
            {
                return;
            }
            ListViewItem item = listView1.SelectedItems[0];
            if (radioButtonWeapon.Checked)
                SaveWeaponDetail(Convert.ToInt32(item.SubItems[0].Text));
            else if (radioButtonArmor.Checked)
                SaveArmorDetail(Convert.ToInt32(item.SubItems[0].Text));
            else if (radioButtonAssist.Checked)
                SaveAssistDetail(Convert.ToInt32(item.SubItems[0].Text));
            else if (radioButtonConsume.Checked)
                SaveSundryDetail(Convert.ToInt32(item.SubItems[0].Text));
        }

        private void buttonRecover_Click(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count == 0)
            {
                return;
            }
            ListViewItem item = listView1.SelectedItems[0];
            if (radioButtonConsume.Checked)
                ShowSundryDetail(Convert.ToInt32(item.SubItems[0].Text));
            else
                ShowEquipmentDetail(Convert.ToInt32(item.SubItems[0].Text));
        }
        private void radioButtonEquip_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButtonWeapon.Checked)
                ShowWeapons();
        }

        private void listView1_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
        {

            if (listView1.SelectedItems.Count == 0)
            {
                return;
            }
            ListViewItem item = listView1.SelectedItems[0];
            if (radioButtonConsume.Checked)
                ShowSundryDetail(Convert.ToInt32(item.SubItems[0].Text));
            else
                ShowEquipmentDetail(Convert.ToInt32(item.SubItems[0].Text));

        }
        private void radioButtonArmor_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButtonArmor.Checked)
                ShowArmors();
        }

        private void radioButtonAssist_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButtonAssist.Checked)
                ShowAssists();
        }

        private void radioButtonConsume_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButtonConsume.Checked)
                ShowSundris();
        }

        private void numericUpDownIcon_ValueChanged(object sender, EventArgs e)
        {
            pictureBoxIcon.Image = Image.FromFile(resourcesDirect + "Item" + numericUpDownIcon.Value + ".png");

        }

        private void buttonAdd_Click(object sender, EventArgs e)
        {

        }

        private void buttonDelete_Click(object sender, EventArgs e)
        {

        }
        

    }
    public class Sundry
    {
        public String name;
        public long num;
        public String dspt;
        public long numEffect;
        public long valueEffect;
        public long numIcon;
        public long price;
        public static int compare(Sundry m1, Sundry m2)
        {
            if (m1.num == m2.num)
                return 0;
            else if (m1.num > m2.num)

                return 1;

            else if (m1.num < m2.num)
                return -1;
            else
                return 0;
        }
    }
    public class Equipment : Sundry
    {
        public long grow;
        public long teamEnable;
        public long type;
        public long valueAbility;
    }
}
